package gew.beans;

import gew.data.DimensionValue;
import gew.data.IndicatorGraphItem;
import gew.data.IndicatorTableItem;
import gew.metadata.CustomSql;
import gew.metadata.Queries;
import gew.system.Constants;
import gew.system.Labels;
import gew.system.RequestParameter;
import gew.utils.SqlUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean(name = "industriesBn")
@RequestScoped
public class IndustriesBean extends BaseBean{

    private RequestParameter rp;

    private String indicatorUrlCodesHS;
    private String indicatorIdsHS;
    private String countryUrlCodesHS;
    private String countryIdsHS;
    private String urlCountryBegin;
    private String urlCountryEnd;
    private String urlIndicatorBegin;
    private String urlIndicatorEnd;
    private String includeTreeCountryInit;
    private String includeTreeCountryChildren;
    private String includeTreeCountrySearch;
    private String includeTreeIndicatorInit;
    private String includeTreeIndicatorChildren;
    private String includeTreeIndicatorSearch;
    private List<DimensionValue> selectedCountries;
    private List<DimensionValue> selectedIndicators;
    private String selectedIndicatorName;
    private String selectedIndicatorUom;
    private String selectedCountryNames;
    private String selectedCountryNamesGen;
    private boolean hasData;

    private ArrayList<IndicatorGraphItem> graphItems;
    private ArrayList<IndicatorTableItem> tableItems;
    List<String> periods;

    @PostConstruct
    public void init() {
        HttpServletRequest r = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        rp = new RequestParameter(r, dvs);
        logRequest(r, Constants.SECTION_INDUSTRIES, rp.getRequestURI());

        // <editor-fold defaultstate="collapsed" desc="TREEs">
        indicatorUrlCodesHS = rp.getIndicatorUrlCode();
        indicatorIdsHS = rp.getIndicatorIdsHS();
        countryUrlCodesHS = rp.getCountryUrlCode();
        countryIdsHS = rp.getCountryIdsHS();
        urlCountryBegin = rp.getContextPath()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_INDUSTRIES)
                .concat(SqlUtils.SLASH)
                .concat(getIndicatorUrlCodesHS())
                .concat(SqlUtils.SLASH);
        urlCountryEnd = SqlUtils.SLASH;
        urlIndicatorBegin = rp.getContextPath()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_INDUSTRIES)
                .concat(SqlUtils.SLASH);
        urlIndicatorEnd = SqlUtils.SLASH
                .concat(countryUrlCodesHS)
                .concat(SqlUtils.SLASH);
        String hierGroupCountry = (new Integer(Constants.HIER_GROUP_COUNTRY)).toString();
        includeTreeCountryInit = rp.getHttpHeader()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TREE)
                .concat(SqlUtils.SLASH)
                .concat(Constants.FILTER_REQUEST_INIT)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_SPACES_NO_CODES_BOLD1)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupCountry)
                .concat(SqlUtils.SLASH)
                .concat(urlCountryBegin.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.AT)
                .concat(countryUrlCodesHS)
                .concat(SqlUtils.AT)
                .concat(urlCountryEnd.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.SLASH)
                .concat(countryIdsHS)
                .concat(SqlUtils.SLASH);
        includeTreeCountryChildren = rp.getHttpHeader()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TREE)
                .concat(SqlUtils.SLASH)
                .concat(Constants.FILTER_REQUEST_CHILDREN)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_SPACES_NO_CODES_BOLD1)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupCountry)
                .concat(SqlUtils.SLASH)
                .concat(urlCountryBegin.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.AT)
                .concat(countryUrlCodesHS)
                .concat(SqlUtils.AT)
                .concat(urlCountryEnd.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_PARENT_ID)
                .concat(SqlUtils.SLASH);
        includeTreeCountrySearch = rp.getHttpHeader()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TREE)
                .concat(SqlUtils.SLASH)
                .concat(Constants.FILTER_REQUEST_SEARCH)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_SPACES_NO_CODES_BOLD1)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupCountry)
                .concat(SqlUtils.SLASH)
                .concat(urlCountryBegin.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.AT)
                .concat(countryUrlCodesHS)
                .concat(SqlUtils.AT)
                .concat(urlCountryEnd.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.SLASH)
                .concat(countryIdsHS)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_SEARCH_TEXT)
                .concat(SqlUtils.SLASH);
        String hierGroupIndicator = (new Integer(Constants.HIER_GROUP_INDICATOR_INDUSTRY)).toString();
        includeTreeIndicatorInit = rp.getHttpHeader()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TREE)
                .concat(SqlUtils.SLASH)
                .concat(Constants.FILTER_REQUEST_INIT)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_CODES_BOLDGROUP)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupIndicator)
                .concat(SqlUtils.SLASH)
                .concat(urlIndicatorBegin.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.AT)
                .concat("")
                .concat(SqlUtils.AT)
                .concat(urlIndicatorEnd.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.SLASH)
                .concat(indicatorIdsHS)
                .concat(SqlUtils.SLASH);
        includeTreeIndicatorChildren = rp.getHttpHeader()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TREE)
                .concat(SqlUtils.SLASH)
                .concat(Constants.FILTER_REQUEST_CHILDREN)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_CODES_BOLDGROUP)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupIndicator)
                .concat(SqlUtils.SLASH)
                .concat(urlIndicatorBegin.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.AT)
                .concat("")
                .concat(SqlUtils.AT)
                .concat(urlIndicatorEnd.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_PARENT_ID)
                .concat(SqlUtils.SLASH);
        includeTreeIndicatorSearch = rp.getHttpHeader()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TREE)
                .concat(SqlUtils.SLASH)
                .concat(Constants.FILTER_REQUEST_SEARCH)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_TEMPLATE_NO_CODES_BOLDGROUP)
                .concat(SqlUtils.SLASH)
                .concat(hierGroupIndicator)
                .concat(SqlUtils.SLASH)
                .concat(urlIndicatorBegin.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.AT)
                .concat("")
                .concat(SqlUtils.AT)
                .concat(urlIndicatorEnd.replace(SqlUtils.SLASH, SqlUtils.VERTICAL_BAR))
                .concat(SqlUtils.SLASH)
                .concat(indicatorIdsHS)
                .concat(SqlUtils.SLASH)
                .concat(Constants.TREE_VAR_SEARCH_TEXT)
                .concat(SqlUtils.SLASH);
        // </editor-fold>

        selectedCountries = dvs.getSelectedValues(rp.getCountryIds(), rp.getLocale(), Queries.CountryDimensionValue);
        selectedIndicators = dvs.getSelectedValues(rp.getIndicatorIds(), rp.getLocale(), Queries.IndicatorDimensionValue);
        selectedIndicatorName = selectedIndicators.size() > 0 ? selectedIndicators.get(0).getName() : SqlUtils.EMPTY;
        selectedIndicatorUom = dvs.getUomById(Queries.IndicatorDimensionValue, rp.getIndicatorIds().size() > 0 ? rp.getIndicatorIds().get(0) : 0, rp.getLocale());
        selectedCountryNames = dvs.getAllNames(Queries.CountryDimensionValue, rp.getCountryIds(), rp.getLocale());
        selectedCountryNamesGen = dvs.getAllNamesGen(Queries.CountryDimensionValue, rp.getCountryIds(), rp.getLocale());

        // <editor-fold defaultstate="expanded" desc="INIT">
        String indicatorIds = rp.getIndicatorIdsCS();
        String countryIds = rp.getCountryIdsCS();
        String q1 = CustomSql.getIndicatorsGraphData(rp.getCountryIds(), countryIds, indicatorIds);
        graphItems = df.getIndicatorGraphItems(q1);
        q1 = CustomSql.getIndicatorsTableValues(countryIds, indicatorIds);
        periods = df.executeQueryToListOfString(q1);
        q1 = CustomSql.getIndicatorsTableData(countryIds, indicatorIds, periods, rp.getLocaleUrlCode());
        tableItems = df.getIndicatorTableItems(q1);
        for (int i = 0; i < tableItems.size(); i++) {
            tableItems.get(i).setCountryName(dvs.getNameById(Queries.CountryDimensionValue, tableItems.get(i).getCountryId(), rp.getLocale()));
        }
        hasData = !graphItems.isEmpty();
        // </editor-fold>
    }

    public String getUI_FOOTER_INDUSTRIES() {
        return Labels.get_UI_FOOTER_INDUSTRIES(rp.getLocale());
    }

    public String getUI_TITLE_COUNTRIES() {
        return Labels.get_UI_TITLE_COUNTRIES(rp.getLocale());
    }

    public String getUI_TITLE_INDICATORS() {
        return Labels.get_UI_TITLE_INDICATORS(rp.getLocale());
    }

    public String getUI_TITLE_PERIOD() {
        return Labels.get_UI_TITLE_PERIOD(rp.getLocale());
    }

    public String getUI_TITLE_COUNTRY() {
        return Labels.get_UI_TITLE_COUNTRY(rp.getLocale());
    }

    public String getUI_TITLE_INDICATOR() {
        return Labels.get_UI_TITLE_INDICATOR(rp.getLocale());
    }

    public String getUI_TITLE_VALUE() {
        return Labels.get_UI_TITLE_VALUE(rp.getLocale());
    }

    public String getPageTitle() {
        return getPageTitle(rp.getLocale(), selectedIndicatorName, selectedIndicatorUom, selectedCountryNames, true);
    }

    public String getShortTitle() {
        return getPageTitle(rp.getLocale(), selectedIndicatorName, selectedIndicatorUom, selectedCountryNames, false);
    }

    public String getLocaleUrlCode() {
        return rp.getLocaleUrlCode();
    }

    public String getIndicatorUrlCodesHS() {
        return indicatorUrlCodesHS;
    }

    public String getIndicatorIdsHS() {
        return indicatorIdsHS;
    }

    public String getCountryUrlCodesHS() {
        return countryUrlCodesHS;
    }

    public String getCountryIdsHS() {
        return countryIdsHS;
    }

    public String getUrlCountryBegin() {
        return urlCountryBegin;
    }

    public String getUrlCountryEnd() {
        return urlCountryEnd;
    }

    public String getUrlIndicatorBegin() {
        return urlIndicatorBegin;
    }

    public String getUrlIndicatorEnd() {
        return urlIndicatorEnd;
    }

    public String getIncludeTreeCountryInit() {
        return this.includeTreeCountryInit;
    }

    public String getIncludeTreeCountryChildren() {
        return this.includeTreeCountryChildren;
    }

    public String getIncludeTreeCountrySearch() {
        return this.includeTreeCountrySearch;
    }

    public String getIncludeTreeIndicatorInit() {
        return this.includeTreeIndicatorInit;
    }

    public String getIncludeTreeIndicatorChildren() {
        return this.includeTreeIndicatorChildren;
    }

    public String getIncludeTreeIndicatorSearch() {
        return this.includeTreeIndicatorSearch;
    }

    public String getSelectedCountryUrl(String urlCode) {
        String url = SqlUtils.excludeUrlCode(urlCode, countryUrlCodesHS);
        return url.isEmpty() ? ""
                : urlCountryBegin
                .concat(SqlUtils.excludeUrlCode(urlCode, countryUrlCodesHS))
                .concat(urlCountryEnd);
    }

    public Collection<DimensionValue> getFilterSelectedItemsCountry() {
        return selectedCountries;
    }

    public Collection<DimensionValue> getFilterSelectedItemsIndicator() {
        return selectedIndicators;
    }

    public String getSelectedIndicatorName() {
        return selectedIndicatorName;
    }

    public Iterable<IndicatorGraphItem> getGraphItems() {
        return graphItems;
    }

    public String getUI_TITLE_INFO() {
        return Labels.get_UI_TITLE_INFO(rp.getLocale());
    }

    public String getUI_INFO_INDICATORS_NO_DATA() {
        return Labels.get_UI_INFO_INDICATORS_NO_DATA(rp.getLocale())
                .replace("[indicator]", selectedIndicatorName)
                .replace("[country_names_gen]", selectedCountryNamesGen);
    }

    public String getNotes() {
        return selectedIndicators.size() > 0 ? selectedIndicators.get(0).getDescription() : SqlUtils.EMPTY;
    }

    public Iterable<IndicatorTableItem> getTableItems() {
        return tableItems;
    }

    public Iterable<String> getPeriods() {
        return periods;
    }

    public String getContextPath() {
        return rp.getContextPath();
    }

    public String getKeyWords() {
        String indicatorKeyWords = dvs.getAllKeyWords(
                Queries.IndicatorDimensionValue,
                rp.getIndicatorIds(),
                rp.getLocale());
        String countryKeyWords = dvs.getAllKeyWords(
                Queries.CountryDimensionValue,
                rp.getCountryIds(),
                rp.getLocale());
        String lastPeriods = SqlUtils.EMPTY;
        for (int i = 0; i < Math.min(4, periods.size()); i++) {
            lastPeriods = lastPeriods
                    .concat(SqlUtils.COMMA)
                    .concat(SqlUtils.SPACE)
                    .concat(periods.get(i));
        }
        return SqlUtils.EMPTY
                .concat(indicatorKeyWords)
                .concat(SqlUtils.COMMA).concat(SqlUtils.SPACE)
                .concat(countryKeyWords)
                .concat(lastPeriods);
    }

    public String getFirstPeriod() {
        return periods.size() > 0 ? periods.get(periods.size() - 1) : SqlUtils.EMPTY;
    }

    public String getLastPeriod() {
        return periods.size() > 0 ? periods.get(0) : SqlUtils.EMPTY;
    }

    public boolean isHasData() {
        return hasData;
    }

    public String getSelectedCountryNamesGen() {
        return selectedCountryNamesGen;
    }

    static String getPageTitle(Locale locale, String indicatorName, String indicatorUom, String countryNames, Boolean includeMainTitle) {
        StringBuilder buf = new StringBuilder();
        buf
                .append(indicatorName);
        if (!SqlUtils.EMPTY.equals(indicatorUom)) {
            buf
                    .append(SqlUtils.COMMA)
                    .append(SqlUtils.SPACE)
                    .append(indicatorUom);
        }
        if (!SqlUtils.EMPTY.equals(countryNames)) {
            buf
                    .append(SqlUtils.SPACE)
                    .append(SqlUtils.HYPHEN)
                    .append(SqlUtils.SPACE)
                    .append(countryNames);
        }
        if (includeMainTitle) {
            buf
                    .append(SqlUtils.SPACE)
                    .append(SqlUtils.VERTICAL_BAR)
                    .append(SqlUtils.SPACE)
                    .append(Labels.get_UI_TITLE_MAIN_SHORT(locale));
        }
        return buf.toString();
    }

    static String getDescription(Locale locale, String selectedIndicatorName, String selectedCountryNamesGen) {
        return Labels.get_UI_DESCRIPTION_SEARCH_INDUSTRIES(locale)
                .replace("[selectedIndicatorName]", selectedIndicatorName)
                .replace("[selectedCountryNamesGen]", selectedCountryNamesGen);
    }

    public String getDescription() {
        return Labels.get_UI_DESCRIPTION_SEARCH_INDUSTRIES(rp.getLocale())
                .replace("[selectedIndicatorName]", selectedIndicatorName)
                .replace("[selectedCountryNamesGen]", selectedCountryNamesGen);
    }

    static String getUrl(String contextPath, String localeUrlCode, String indicatorUrlCode, String countryUrlCode) {
        StringBuilder buf = new StringBuilder();
        buf
                .append(contextPath)
                .append(SqlUtils.SLASH)
                .append(localeUrlCode)
                .append(SqlUtils.SLASH)
                .append(Constants.SECTION_INDUSTRIES)
                .append(SqlUtils.SLASH)
                .append(indicatorUrlCode)
                .append(SqlUtils.SLASH)
                .append(countryUrlCode);
        return buf.toString();
    }

    public String getSelectedIndicatorUom() {
        return selectedIndicatorUom;
    }

}
